<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Users_model extends MY_Model {
	
	protected $users;
	
	function __construct() {
		parent::__construct();
	}
	
	function table_name() {
		return "users";
	}
	
	function get_objects($filters = array(), $counting = false) {
		if (isset($this->CI->cache)) {
			$caching = true;
			$cache_key = 'ci--USER_'.md5(serialize($filters));
			
			if ($counting) {
				$cache_key = 'ci--USER_COUNT_'.md5(serialize($filters));
			}
			
			if ($return = $this->cache->file->get($cache_key)) {
				return ($return == 'empty_cache') ? FALSE : $return;
			}
		} else {
			$caching = false;
		}
		
		$this->db->select('u.id, u.username, u.password, u.email, u.is_active, u.day_out_vip, u.value_token, u.id_levels, u.id_objects, u.status, u.created, 
			uin.*,
			cc.name AS category_name,cc.alias AS category_alias, 
			mca.name as career_name, mpr.name as province_name, mlv.name as level_name')
				->join('users_information AS uin','uin.id_user = u.id','inner')
				->join('categories AS cc','cc.category_id = uin.category_id','left')
				->join('m_careers AS mca','mca.id = uin.id_careers','left')
				->join('m_provinces AS mpr','mpr.id = uin.id_provinces','left')
				->join('m_levels AS mlv','mlv.id = u.id_levels','left')
				->from('users as u');
		$this->db->where('u.is_delete', 0);
		$this->db->where('uin.is_delete', 0);
		
		// in table user		
		if (isset($filters['id'])) {
			$this->db->where('u.id', $filters['id']);
		}
		
		if (isset($filters['username'])) {
			$this->db->where('u.username', $filters['username']);
		}

		if (isset($filters['password'])) {
			$this->db->where('u.password', $filters['password']);
		}
		
		if (isset($filters['email'])) {
			$this->db->where('u.email', $filters['email']);
		}

		if (isset($filters['is_active'])) {
			$this->db->where('u.is_active', $filters['is_active']);
		}
		
		if (isset($filters['day_out_vip'])) {
			$this->db->where('u.day_out_vip', $filters['day_out_vip']);
		}
		
		if (isset($filters['value_token'])) {
			$this->db->where('u.value_token', $filters['value_token']);
		}
		
		if (isset($filters['id_levels'])) {
			$this->db->where('u.id_levels', $filters['id_levels']);
		}

		if (isset($filters['id_objects'])) {
			$this->db->where('u.id_objects', $filters['id_objects']);
		}
		
		if (isset($filters['id_status'])) {
			$this->db->where('u.id_status', $filters['id_status']);
		}

		if (isset($filters['status'])) {
			$this->db->where('u.status', $filters['status']);
			$this->db->where('uin.status', $filters['status']);
		}

		// in table user_infomation
		if (isset($filters['id_information'])) {
			$this->db->where('uin.id', $filters['id_information']);
		}

		if (isset($filters['id_information_other'])) {
			$this->db->where('uin.id !=', $filters['id_information']);
		}

		if (isset($filters['name'])) {
			$this->db->like('uin.name', $filters['name']);
		}

		if (isset($filters['alias'])) {
			$this->db->where('uin.alias', $filters['alias']);	
		}
		
		if (isset($filters['category_id'])) {
			$this->db->where('uin.category_id', $filters['category_id']);
		}

		if (isset($filters['id_province'])) {
			$this->db->where('uin.id_province', $filters['id_province']);
		}

		if (isset($filters['id_careers'])) {
			$this->db->where('uin.id_careers', $filters['id_careers']);	
		}
		
		if (isset($filters['is_main'])) {
			$this->db->where('uin.is_main', $filters['is_main']);
		}

		// limit
		if (isset($filters['limit'])) {
			$offset = (isset($filters['page'])) ? $filters['page'] : 0;
			$this->db->limit($filters['limit'], $offset);
		}
		
		$this->db->order_by('u.id','DESC');
		
		$result = $this->db->get();
		if ($counting !=false) {
			$rows = $result->num_rows();
			$result->free_result();
			
			if ($caching == TRUE) {
				$this->CI->cache->file->save($cache_key, $rows, (5*60));	
			}
			
			return $rows;
		}
		if ($result->num_rows() == 0) {
			return false;
		}
		
		$list = array();
		foreach ($result->result_array() as $row) {
			$row['link'] = create_url($row['category_alias'] .'/'. $row['alias'], array('rewrite'=>true));
			$row['created'] = fdate($row['created'], "%d/%m/%Y");
			$list[] = $row;
		}

		if ($caching) {
			$this->CI->cache->file->save($cache_key, $list);
		}
		$result->free_result();
		
		return $list;
	}
	
	function get_object_by_id( $id ) {
		if (!isset($this->users[$id])) {
			$object = $this->get_objects( array('id' => $id));
			$this->users[$id] = $object;
		} else {
			$object = $this->users[$id];
		}
		if (empty($object)) {
			return false;
		}
		
		return $object[0];
	}
	
	function get_object_by_alias($type_alias) {
		$filters = array('alias' => $type_alias);
		$result = $this->get_objects($filters);
		if (empty($result)) {
			return false;
		}
		return $result[0];
	}
	
	function get_objects_options() {
		$object_options = array();
		$list = $this->get_objects(array());
		if (is_array($list) && count($list) > 0) {
			foreach ($list as $value) {
				$object_options[$value['id']] = $value['name'];
			}
		}
		return $object_options;
	}
	
	function get_objects_count($filters = array()) {
		return $this->get_objects($filters, true);
	}
	
	function insert($data, $id_objects = 1) {
		$this->load->helper('string');
        $token = random_string('unique', 15);

		$arrUser = array();
		$arrUser['username'] = $data['username'];
		$arrUser['password'] = $data['password'];
		$arrUser['email'] = $data['email'];
		$arrUser['is_active'] = $data['is_active'];
		$arrUser['day_out_vip'] = $data['day_out_vip'];
		$arrUser['value_token'] = $token;
		$arrUser['id_levels'] = $data['id_levels'];
		$arrUser['id_objects'] = $id_objects;
		$arrUser['id_status'] = 1;
		$arrUser['status'] = $data['status'];
		$arrUser['is_delete'] = 0;

		$this->db->trans_start();

		$id_user = $this->save($arrUser, $this->table_name());

		$arrUserInfo = array();
		$arrUserInfo['id_user'] = $id_user;
		$arrUserInfo['contact_email'] = $data['contact_email'];
		$arrUserInfo['name'] = $data['name'];
		$arrUserInfo['alias'] = $data['alias'];
		$arrUserInfo['about'] = $data['about'];
		$arrUserInfo['logo'] = $data['logo'];
		$arrUserInfo['avatar'] = $data['avatar'];
		$arrUserInfo['contact_preson'] = $data['contact_preson'];
		$arrUserInfo['website'] = $data['website'];
		$arrUserInfo['tel'] = $data['tel'];
		$arrUserInfo['fax'] = $data['fax'];
		$arrUserInfo['yahoo'] = $data['yahoo'];
		$arrUserInfo['skype'] = $data['skype'];
		$arrUserInfo['facebook'] = $data['facebook'];
		$arrUserInfo['category_id'] = 39;
		$arrUserInfo['address'] = $data['address'];
		$arrUserInfo['local_maps'] = $data['local_maps'];
		$arrUserInfo['open_time'] = $data['open_time'];
		$arrUserInfo['id_provinces'] = $data['id_provinces'];
		$arrUserInfo['id_careers'] = $data['id_careers'];
		$arrUserInfo['is_main'] = 1;
		$arrUserInfo['contents'] = $data['contents'];
		$arrUserInfo['status'] = $data['status'];
		$arrUserInfo['is_delete'] = 0;
		$arrUserInfo['slide1'] = $data['slide1'];
		$arrUserInfo['slide2'] = $data['slide2'];
		$arrUserInfo['slide3'] = $data['slide3'];
		$arrUserInfo['slide4'] = $data['slide4'];

		$id_user_info = $this->save($arrUserInfo, 'users_information');

		$this->db->trans_complete();
		
		return true;
	}
	
	function update($data, $id, $id_objects = 1) {
		$arrUser = array();
		$arrUser['username'] = $data['username'];
		$arrUser['password'] = $data['password'];
		$arrUser['email'] = $data['email'];
		$arrUser['day_out_vip'] = $data['day_out_vip'];
		$arrUser['id_levels'] = $data['id_levels'];
		$arrUser['status'] = $data['status'];
		$arrUser['id_objects'] = $id_objects;

		$this->db->trans_start();
		parent::update($arrUser, array('id'=>$id), $this->table_name());

		$arrUserInfo = array();
		$arrUserInfo['contact_email'] = $data['contact_email'];
		$arrUserInfo['name'] = $data['name'];
		$arrUserInfo['alias'] = $data['alias'];
		$arrUserInfo['about'] = $data['about'];
		$arrUserInfo['logo'] = $data['logo'];
		$arrUserInfo['avatar'] = $data['avatar'];
		$arrUserInfo['contact_preson'] = $data['contact_preson'];
		$arrUserInfo['website'] = $data['website'];
		$arrUserInfo['tel'] = $data['tel'];
		$arrUserInfo['fax'] = $data['fax'];
		$arrUserInfo['yahoo'] = $data['yahoo'];
		$arrUserInfo['skype'] = $data['skype'];
		$arrUserInfo['facebook'] = $data['facebook'];
		$arrUserInfo['address'] = $data['address'];
		$arrUserInfo['local_maps'] = $data['local_maps'];
		$arrUserInfo['open_time'] = $data['open_time'];
		$arrUserInfo['id_provinces'] = $data['id_provinces'];
		$arrUserInfo['id_careers'] = $data['id_careers'];
		$arrUserInfo['contents'] = $data['contents'];
		$arrUserInfo['status'] = $data['status'];
		$arrUserInfo['slide1'] = $data['slide1'];
		$arrUserInfo['slide2'] = $data['slide2'];
		$arrUserInfo['slide3'] = $data['slide3'];
		$arrUserInfo['slide4'] = $data['slide4'];

		parent::update($arrUserInfo, array('id_user'=> $id), 'users_information');

		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
		{
		    $this->db->trans_rollback();
		    return false;
		}
		else
		{
		    $this->db->trans_commit();
		    return true;
		}

		return true;
	}
	
	function delete($id) {
		parent::update(array('is_delete' => 1), array('id' => $id), $this->table_name());
		parent::update(array('is_delete' => 1), array('id_user' => $id), 'users_information');
		return true;
	}
	
	function change_status($id, $change = 1) {
		parent::update(array('status' => $change), array('id' => $id), $this->table_name());
		parent::update(array('status' => $change), array('id_user' => $id), 'users_information');
        return true;
    }
}